package cn.myfirefly.moming.portal.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;

import cn.myfirefly.moming.portal.entity.MMSystemAccess;
import cn.myfirefly.moming.portal.model.SystemAccessDto;
import cn.myfirefly.moming.portal.model.SystemAccessFilterDto;
import cn.myfirefly.moming.portal.service.SystemAccessService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.FileTransportUtil;
import cn.myfirefly.utils.PropUtil;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping(value = "/systemAccess")
public class SystemAccessController extends BaseController {
	
	private final static String INDEX = "systemaccess/index";
	private final static String EDIT = "systemaccess/edit";
	
	@Autowired
	SystemAccessService systemAccessService;
	
	@RequestMapping(value = "/index.do")
	public String toIndex(HttpServletRequest request){
		return INDEX;
	}
	
	@RequestMapping(value = "/list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<MMSystemAccess> list(SystemAccessFilterDto filterDto){
		PageResultDto<MMSystemAccess> dto = systemAccessService.getSystemAccessList(filterDto);
		return dto;
	}
	
	@RequestMapping(value="/edit.do")
	public String edit(HttpServletRequest request, HttpServletResponse response){
		return EDIT;
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public ResultDto saveOrUpdate(HttpServletRequest request,SystemAccessDto dto){
		if (dto.getSystemId() == null) {
			return error("系统编号不能为空");
		}
		String msg = systemAccessService.saveOrUpdate(dto);
		if (!StrUtil.equals(msg, Const.ResultCode.SUCCESS)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping(value = "/changeShow.do")
	@ResponseBody
	public ResultDto changeShow(HttpServletRequest request){
		try {
			String id = request.getParameter("id");
			String show = request.getParameter("show");
			if("open".equals(show)){
				systemAccessService.changeStatus(Integer.valueOf(id),'1');
			}else if("close".equals(show)){
				systemAccessService.changeStatus(Integer.valueOf(id),'0');
			}else{
				return error("参数不合法");
			}
		} catch (NumberFormatException e) {
			return error("参数不合法");
		}
		
		return success();
	}
	
	@RequestMapping(value = "/delete.do")
	@ResponseBody
	public ResultDto delete(HttpServletRequest request,HttpServletResponse response){
		try {
			String id = request.getParameter("id");
			String msg = systemAccessService.delete(Integer.valueOf(id));
			if (!StrUtil.equals(msg, Const.ResultCode.SUCCESS)) {
				return error(msg);
			}
		} catch (NumberFormatException e) {
			return error("参数不合法");
		}
		return success();
	}
	
	@RequestMapping(value = "/getSystemAccess.do")
	@ResponseBody
	public ResultDto getSystemAccess(HttpServletRequest request,HttpServletResponse response){
		MMSystemAccess systemAccess = null;
		try {
		    String id = request.getParameter("id");
		    systemAccess = systemAccessService.getSystemAccess(Integer.valueOf(id));
		} catch (NumberFormatException e) {
			return error("参数不合法");
		}
		return success(systemAccess);
	}
	
	@RequestMapping(value="/deal_pic.do")
	@ResponseBody
	public JSONObject dealPic(HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest request2){
		MultipartFile file = request2.getFile("file");
		String realPath = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.PIC_REAL_PATH);
		String showPath = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.PIC_URL);
		JSONObject obj = new JSONObject();
		String targetName = FileTransportUtil.saveFile(realPath, file);
		if(StrUtil.notBlank(targetName)){
			obj.put("statusCode", 200);
			obj.put("message", "上传成功");
			obj.put("filename", showPath+"/"+targetName);
		}else{
			obj.put("statusCode", 800);
			obj.put("message", "上传失败");
		}
		return obj;
	}
}
